// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

cr.define('downloads', function () {

      Polymer({
            is: 'downloads-login',
            properties: {
                user: {
                    type: Object,
                    value: {}
                }, 
                isRegister:{
                    type:Boolean,
                    value:false
                } ,
                isRegist:{
                    type:Boolean,
                    value:false
                } ,
                isLogin:{
                    type:Boolean,
                    value:false
                } ,
                isSms:{
                    type:Boolean,
                    value:false
                } ,
                isPwd:{
                    type:Boolean,
                    value:false
                },
                changeSms: {
                    type: String,
                    value: 'true'
                },
                changePwd: {
                    type: String,
                    value: "false"
                },
                isFinPwd:{
                    type:Boolean,
                    value:false
                } ,
                isSmsLogin:{
                    type:Boolean,
                    value:false
                } ,
                isAccountLogin:{
                    type:Boolean,
                    value:false
                } ,
            },
            openDialog: function(){
                //0为注册false   1 为注册  true 
              

                this.$.dialog.open();
                if(this.isRegister){
                    // 没有注册 
                    this.isRegist=true;
                    this.isLoin=false;
                    this.isFinPwd=false;
                    console.log("没有注册");
                }else{
                    // 注册成功 
                    console.log("注册过");
                    this.isRegist=false;
                    this.isLoin=true;
                    this.isSms=true ;
                    this.changeSms = "true";
                    this.changePwd = "false";                   
                    this.isPwd=false; 
                    this.isFinPwd=false;
                    this.isSmsLogin=true;
                }

                 
            },
            registerpage:function(){
                this.isRegist=true;
                this.isLoin=false;
                this.isFinPwd=false;
            },
            loginpage:function(){
                this.isRegist=false;
                this.isLoin=true;
                this.isFinPwd=false;
            },
           
            accountLogin:function(){
                this.isSms=false;
                this.changeSms = "false"
                this.isPwd=true;
                this.changePwd = "true";
                this.isFinPwd=false;
                this.isSmsLogin=false;
                this.isAccountLogin=true;
            },
            smsLogin:function(){
                this.isSms=true ;
                this.changeSms = "true";
                this.isPwd=false;
                this.changePwd = "false";
                this.isFinPwd=false;
                this.isSmsLogin=true;
                this.isAccountLogin=false;
            },            
            findPwd:function(){
                this.isRegist=false;
                this.isLoin=false;
                this.isFinPwd=true;
            },
           
            backLogin:function(){
                this.isLoin=true;
                this.isSms=false;
                this.changeSms = "false";
                this.isPwd=true;
                this.changePwd = "true";
                this.isFinPwd=false;
            },
            ready: function () {
             // console.log("1111");
                
            },
          
            login: function(){

                var inputs = document.querySelectorAll('#login input'),
                    inputs_length = inputs.length,
                    is_valid = true;

                for( var i = 0; i < inputs_length; i++ ) {
                    is_valid = inputs[i].validate();
                    inputs[i].focus();
                }

                if(is_valid) {
                    this.$.ajax.url = "http://127.0.0.1:8080/login";
                    this.$.ajax.params = this.user;
                    this.$.ajax.crossDomain= false,
                    this.$.ajax.options= {
                        xhrFields: {
                            withCredentials: true
                        }
                    };
                    this.$.ajax.generateRequest();

                }
            },
            handleResponse: function(res) {
                  console.log(111);
                  var res = res.detail.response;
  
                  console.log(res);
                  console.log(222);
  
                  if(res) {
                      sessionStorage.setItem('id', res.id);
                      sessionStorage.setItem('email', res.email)
                      sessionStorage.setItem('token', res.token);
                      window.location = 'makesomenoice';
                  }
            }
            
      });


});
